home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 10901 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.2 KB

  1. Path: solon.com!not-for-mail
  2. From: seebs@solutions.solon.com (Peter Seebach)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: 16-bit memset?
  5. Date: 20 Mar 1996 13:02:59 -0600
  6. Organization: Usenet Fact Police (Undercover)
  7. Message-ID: <4ipkp3$1sc@solutions.solon.com>
  8. References: <joules-1803962243060001@badboy.mit.edu> <4ill6r$qnm@nntp.interaccess.com> <DoJABC.Hy7@iquest.net>
  9. NNTP-Posting-Host: solutions.solon.com
  10.  
  11. In article <DoJABC.Hy7@iquest.net>, Doug Miller <dlmiller@iquest.net> wrote:
  12. >+Julian Orbanes wrote:
  13. >+>I am looking quick way to set a series of 16-bit values to one value.
  14.  
  15. >+>Analagous to how memset set works with 8-bit values. (For graphics
  16. >+>purposes).
  17.  
  18. >char    *bigstuff;
  19.  
  20. >/* assuming that:                            */
  21. >/* 1) you have allocated storage for bigstuff                */
  22. >/* 2) it is a null-terminated string of the desired FINAL length    */
  23. >/* 3) you have loaded the value you wish to propagate into the first 2 bytes */
  24.  
  25. >strncpy (bigstuff + 2, bigstuff, (strlen (bigstuff) - 2));
  26.  
  27. Fascinating, but utterly wrong; it is illegal to depend on the semantics of
  28. copying a string onto itself, and the obvious implementation of strncpy will
  29. do precisely what you don't want here.
  30.  
  31. Could you kindly shut up until you know C?
  32.  
  33. (For the naive reader: The intent is that strncpy loops from the beginning,
  34. copying bytes, until it runs out, and that it will keep copying [pos] to
  35. [pos+2], so it will do "the right thing".  The obvious failure is that if
  36. either byte of the 16 bit value is 0, you lose.  The more subtle one is that
  37. strncpy can be implemented quite well in several other ways that break
  38. horribly on this.)
  39.  
  40. This is probably the worst way to implement the operation, and would in most
  41. cases be dramatically slower than looping on 16-bit values and assigning, even
  42. on systems where it works, because it'll waste a lot of time checking every
  43. byte to see if it's a 0 - strncpy *does* have to check for the 0 byte, because
  44. it 0 fills past the end of the original string.
  45.  
  46. -s
  47. -- 
  48. Peter Seebach - seebs@solon.com - Copyright 1996 Peter Seebach.
  49. C/Unix wizard -- C/Unix questions? Send mail for help.  No, really!
  50. FUCK the communications decency act.  Goddamned government.  [literally.]
  51. The *other* C FAQ - http://www.solon.com/~seebs/c/c-iaq.html
  52.